package com.ujs.edu.demo.controller;

import com.sun.mail.imap.protocol.MODSEQ;
import com.ujs.edu.demo.config.NeedLogin;
import com.ujs.edu.demo.dao.BcCartMapper;
import com.ujs.edu.demo.dataobject.BcCart;
import com.ujs.edu.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.List;

@Controller
public class AddCartController {

    @Autowired
    private BcCartMapper bcCartMapper;

    @RequestMapping(path = "addcart")
    public ModelAndView addcart(){
        ModelAndView view=new ModelAndView();
        List<BcCart> bcCarts=bcCartMapper.selectAll();
        view.addObject("carts",bcCarts);
        view.setViewName("addcart");
        view.setViewName("addcart");
        return view;
    }

    //添加分类
    @RequestMapping(path = "/addcartForm")
    @ResponseBody
    public Result addcartForm(BcCart bcCart){

        Result result=new Result();

        int bc=bcCartMapper.insert(bcCart);
        if(bc==1){
            result.setStatus(1);
            result.setMsg("插入成功");
        }else{
            result.setStatus(0);
            result.setMsg("插入失败");
        }
        return  result;
    }

    //修改数据回填
    @RequestMapping(path = "/modputInform")
    public ModelAndView modputInfrom(@RequestParam("cartId") Integer cartId){
        ModelAndView view=new ModelAndView();

        BcCart bcCart=bcCartMapper.selectByPrimaryKey(cartId);
        view.addObject("modcart",bcCart);
        view.setViewName("modcart");
        return view;
    }

    //修改数据保存
    @RequestMapping(path = "modcartput")
    @ResponseBody
    public Result modcartput(BcCart bcCart){

        Result result=new Result();

        int bc=bcCartMapper.updateByPrimaryKey(bcCart);
        if (bc==1){
            result.setStatus(1);
            result.setMsg("修改成功");
        }else {
            result.setStatus(0);
            result.setMsg("修改失败");
        }
        return result;
    }


    //删除分类
    //删除管理员
    @RequestMapping(path = "deletecart")
    @ResponseBody
    public Result deleteAdmin(@RequestParam("cartId") Integer cartId){
        System.out.println("进入后台");
        Result result=new Result();

        RedirectView view=new RedirectView();

        int del=bcCartMapper.deleteByPrimaryKey(cartId);
        System.out.println(del);
        if (del==1){
            result.setMsg("成功");
            result.setStatus(1);
        }else {
            result.setStatus(0);
            result.setMsg("失败");
        }
        view.setUrl("/cart");
        return result;
    }
}
